package com.h5ve.wxmp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h5ve.base.api.R;
import com.h5ve.base.constant.AppConstants;
import com.h5ve.base.util.StrUtil;
import com.h5ve.wxmp.constant.ConfigConstant;
import com.h5ve.wxmp.entity.WxReply;
import com.h5ve.wxmp.service.WxReplyService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息自动回复
 *
 * @author www.joolun.com
 * @date 2019-04-18 15:40:39
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstants.APP_WXMP + "/wxReply")
public class WxReplyController {

    private final WxReplyService wxReplyService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param wxReply 消息自动回复
     * @return
     */
    @GetMapping("/page")
    @SaCheckPermission("wxReply_view")
    public R getWxAutoReplyPage(Page page, WxReply wxReply) {
        return R.ok(wxReplyService.page(page, Wrappers.query(wxReply)));
    }


    /**
     * 通过id查询消息自动回复
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    @SaCheckPermission("wxReply_get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(wxReplyService.getById(id));
    }

    /**
     * 新增消息自动回复
     *
     * @param wxReply 消息自动回复
     * @return R
     */
    @PostMapping
    @SaCheckPermission("wxReply_add')")
    public R save(@RequestBody WxReply wxReply) {
        this.jude(wxReply);
        return R.ok(wxReplyService.save(wxReply));
    }

    /**
     * 修改消息自动回复
     *
     * @param wxReply 消息自动回复
     * @return R
     */
    @PutMapping
    @SaCheckPermission("wxReply_edit')")
    public R updateById(@RequestBody WxReply wxReply) {
        this.jude(wxReply);
        return R.ok(wxReplyService.updateById(wxReply));
    }

    /**
     * 通过id删除消息自动回复
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("wxReply_del')")
    public R removeById(@PathVariable String id) {
        return R.ok(wxReplyService.removeById(id));
    }

    /**
     * //校验参数
     *
     * @param wxReply
     */
    public void jude(WxReply wxReply) {
        if (ConfigConstant.WX_AUTO_REPLY_TYPE_2.equals(wxReply.getType())) {
            Wrapper<WxReply> queryWrapper = Wrappers.<WxReply>lambdaQuery()
                    .eq(WxReply::getReqType, wxReply.getReqType());
            List<WxReply> list = wxReplyService.list(queryWrapper);
            if (!StrUtil.isEmptyIfNum(wxReply.getId())) {
                if (list != null && list.size() == 1) {
                    if (!list.get(0).getId().equals(wxReply.getId())) {
                        throw new RuntimeException("请求消息类型重复");
                    }
                }
                if (list != null && list.size() > 1) {
                    throw new RuntimeException("请求消息类型重复");
                }
            }
        }
        if (ConfigConstant.WX_AUTO_REPLY_TYPE_3.equals(wxReply.getType())) {
            Wrapper<WxReply> queryWrapper = Wrappers.<WxReply>lambdaQuery()
                    .eq(WxReply::getReqKey, wxReply.getReqKey())
                    .eq(WxReply::getRepType, wxReply.getRepMate());
            List<WxReply> list = wxReplyService.list(queryWrapper);
            if (list != null && list.size() == 1) {
                if (!list.get(0).getId().equals(wxReply.getId())) {
                    throw new RuntimeException("关键词重复");
                }
            }
            if (list != null && list.size() > 1) {
                throw new RuntimeException("关键词重复");
            }
        }
    }
}
